package controllers;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import view.HRUpEmpDetGui;
import model.Employee;
import model.Envelope;
import client.CcrmApp;

/**
 * This Class is a Controller for Human Resources, which extends AbstractTransfer
 * it's purpose is to update employee details daily by a human resources employee
 * @author gili
 *
 */
public class HRupdateEmployeeDetalesController extends AbstractTransfer {
	
	private HRcontroller prevCon;
	private HRUpEmpDetGui HRugdateView;
/**
 * 	
 * @param ar is gui instance of update employee detales gui
 * @param lastCont is previous controller instance
 */
	public HRupdateEmployeeDetalesController(HRUpEmpDetGui ar,HRcontroller lastCont){
		HRugdateView = ar;
		prevCon = lastCont;
		
		//connect listeners to buttons
		HRugdateView.addDrawDataActionListener(new DrawDataListener());
		HRugdateView.addCancelActionListener(new cancelListener());
		HRugdateView.addOkActionListener(new OkListener());
	}
	/**
	 * 
	 *  @param message is an object received from server 
	 *  
	 */
	 public void handleDBResult(Object message) {
		 if (message instanceof Employee){
			Employee emp = (Employee)message;
			
			//set text to all fields to drawn data from database 
		 	HRugdateView.getEmployeeIdTxt().setText(emp.getUid());
		 	HRugdateView.getLastNameTxt().setText(emp.getLasttname());
		 	HRugdateView.getFirstNameTxt().setText(emp.getFirstname());
		 	HRugdateView.getPasswordTxt().setText(emp.getUpassword());
		 	HRugdateView.getPositionComboBox().setSelectedIndex(emp.getPosition());
		 	
		 	//open all fields to edit by human resources employee that wishes to update details
		 	HRugdateView.getPositionComboBox().setEditable(true);
		 	HRugdateView.getLastNameTxt().setEditable(true);
		 	HRugdateView.getFirstNameTxt().setEditable(true);
		 	HRugdateView.getPasswordTxt().setEditable(true);
		 	HRugdateView.getEmployeeNumberTxt().setEditable(false);
		 	}
		 else if (message instanceof String){
			 
		// set Appropriate message
			 String msg = (String)message;
			 if (msg.equals("Update succeeded")){
				 System.out.println("Update succeeded");
				 HRugdateView.setWarningMes("Update succeeded");
				 HRugdateView.getEmployeeIdTxt().setEditable(true);
				 HRugdateView.getEmployeeNumberTxt().setEditable(true);
				 HRugdateView.ClearText();
			 }
			 else if (msg.equals("employee not found")){
				 HRugdateView.setWarningMes("employee not found");
			 }
		 }
		 else {
			 HRugdateView.setWarningMes("Undefined Error!");
		 }
	 }

/**
 * 
 * getters and setters for this class
 */
	 
	public HRupdateEmployeeDetalesController getHRupdateEmployeeDetalesController(){
		return this;
	}

	public HRcontroller getPrevCon() {
		return prevCon;
	}

	public HRUpEmpDetGui getHRugdateView() {
		return HRugdateView;
	}
/**
 * 
 * This class is a listener for the "draw data" button
 * It implements interface ActionListener
 * @author gili
 *
 */
	class DrawDataListener implements ActionListener{
/**
 * 
 * this method gets the employee number and sends in to the server, to draw the employee details to be updated later
 * by the human resources employee, if employee number is entered, and displays warning message otherwise 
 *  
 */
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String msg= new String("");
			Envelope ev1;
			try{
				HRugdateView.setWarningMesFalse();
				String empNum = new String( HRugdateView.getEmployeeNumberTxt().getText());
				empNum.trim();
				if(empNum.equals("") != true){
					msg ="drawData";
					ev1 = new Envelope(empNum,msg);
					sendToServer(ev1);	
					CcrmApp.clien.setCurrObj(getHRupdateEmployeeDetalesController());
					}
				else
					
					//display warning message
					HRugdateView.setWarningMes("Please Enter Employee Id");
			    }
				catch(Exception e1){
					e1.printStackTrace(); 
				}
		}
	}
/**
 * This class is a listener for the "cancel" button
 * It implements interface ActionListener 
 * disposes current gui and displays previous gui	
 * @author gili
 *
 */
	class cancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			HRugdateView.dispose();
			prevCon.getHRview().setVisible(true);
			
		}
	}

/**
 * This class is a listener for the "OK" button
 * It implements interface ActionListener 
 * @author gili
 *
 */
	class OkListener implements ActionListener{
/**
 * this method draws data from all the fields, generate a model of class Employee with the data
 * and sends to server to update if all field are entered, and displays warning message otherwise
 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			HRugdateView.getEmployeeNumberTxt().setEditable(false);
			Employee emp = new Employee();
			
			//generate employee from the field's data
			emp.setEmployee_id(Integer.parseInt(HRugdateView.getEmployeeNumberTxt().getText()));
			emp.setLasttname(HRugdateView.getLastNameTxt().getText().trim());
			emp.setFirstname(HRugdateView.getFirstNameTxt().getText().trim());
			emp.setUpassword(HRugdateView.getPasswordTxt().getText().trim());
		 	emp.setUid(HRugdateView.getEmployeeIdTxt().getText().trim());
		 	emp.setPosition(HRugdateView.getPositionComboBox().getSelectedIndex());
			Envelope ev1;
			String msg=new String();
			try{
				HRugdateView.setWarningMesFalse();
				if(!(emp.getLasttname().equals("")||emp.getFirstname().equals("")||emp.getUpassword().equals("")||emp.getUid().equals("")||emp.getPosition()==0)){
					msg ="updateData";
					ev1 = new Envelope(emp,msg);
					sendToServer(ev1);	
					CcrmApp.clien.setCurrObj(getHRupdateEmployeeDetalesController());
					}
				else
					HRugdateView.setWarningMes("Error! One or more of the detaild are missing");
			    }
				catch(Exception e1){
					e1.printStackTrace(); 
				}
		}
		
	}
}
